package com.jieao.template.config.shiroConfig;

import com.jieao.template.common.util.JsonUtils;
import com.jieao.template.entity.pub.JsonData;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * @author Jzw
 * @className ShiroFormAuthenticationFilter
 * @since 2020-03-24 20:59
 */
@Slf4j
public class ShiroFormAuthenticationFilter extends FormAuthenticationFilter
{

    @Override
    public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)
    {
        //Always return true if the request's method is OPTIONS
        if (request instanceof HttpServletRequest)
        {
            if (((HttpServletRequest) request).getMethod().toUpperCase().equals("OPTIONS"))
            {
                return true;
            }
        }
        return super.isAccessAllowed(request, response, mappedValue);

    }

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception
    {
        if (isLoginRequest(request, response))
        {
            if (isLoginSubmission(request, response))
            {
                if (log.isTraceEnabled())
                {
                    log.trace("Login submission detected. Attempting to execute login.");
                }

                return executeLogin(request, response);
            }
            else
            {
                if (log.isTraceEnabled())
                {
                    log.trace("Login page view.");
                }

                return true;
            }
        }
        else
        {
            HttpServletRequest req = (HttpServletRequest) request;
            HttpServletResponse resp = (HttpServletResponse) response;
            if (req.getMethod().equals(RequestMethod.OPTIONS.name()))
            {
                resp.setStatus(HttpStatus.OK.value());
                return true;
            }
            else
            {
                if (log.isTraceEnabled())
                {
                    log.trace("Attempting to access a path which requires authentication. Forwarding to the Authentication url [{}]", getLoginUrl());
                }
                //在这里实现自己想返回的信息，其他地方和源码一样就可以了
                resp.setHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
                resp.setHeader("Access-Control-Allow-Credentials", "true");
                resp.setContentType("application/json; charset=utf-8");
                resp.setCharacterEncoding("UTF-8");
                PrintWriter out = resp.getWriter();
                out.println(JsonUtils.objectToJson(JsonData.noLogin()));
                out.flush();
                out.close();
                return false;
            }
        }
    }

}